package commands;

import logic.*;
import storage.*;

public class Add {

	public String addTask(String title, String deadline) {
		Task tempTask = new Task();
		int nextId = TaskMemoryController.getNextId();
		tempTask.setId(nextId); // assertion?
		assert tempTask.getId() == nextId;

		// Title
		tempTask.setTitle(title);
		Parser p = new Parser(deadline);
		if (p.isInAcceptableFormat()) {
			tempTask.setDeadline(p.getDate());
			if (p.isRepetitive()) {
				tempTask.setAmount(p.getNumberOfOccurrences());
				tempTask.setInterval(p.getPeriod());
			}
		} else
			return ("Invalid Date Format. Please re-enter Date");

		// Priority set by default
		tempTask.setPriority(TaskPriority.MEDIUM);

		// Status set by default
		tempTask.setStatus(TaskStatus.INCOMPLETE);

		Task createdTask = TaskDirector.addTask(tempTask);

		UndoRedoManager undoRedoManager = new UndoRedoManager();
		if (undoRedoManager.isNewCommand()) {
			UndoRedoDetails undoDetail_ = new UndoRedoDetails();
			undoDetail_.setDetails("add", tempTask.getId(), tempTask, "", "", "");
			undoRedoManager.storeChanges(undoDetail_);
		}

		return "Task [" + createdTask.getTitle() + "] on "
				+ createdTask.getDeadline() + " created.";
	}
}
